﻿using Windows.UI;
using LinkPad.Shapes;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Input;


namespace LinkPad.Handlers
{
    public class CanvasActionHandler : ICanvaActionHandler, IHasColor
    {
        #region Constructors

        public CanvasActionHandler(ShapeManager shapeManager)
        {
            ShapeManager = shapeManager;
        }

        ~CanvasActionHandler()
        {
            Deactivate();
        }

        #endregion

        #region Properties

        #region IHasColor members

        public virtual bool HasColor
        {
            get { return true; }
        }

        public virtual Color MainColor
        {
            get;
            set;
        }

        #endregion

        public ShapeManager ShapeManager { get; private set; }

        public Canvas MyCanvas
        {
            get { return ShapeManager.Canvas; }
        }

        #endregion

        #region Methods

        #region ICanvasActionHandler members

        public virtual void Activate()
        {
            Attach();
        }

        public virtual void Deactivate()
        {
            Detach();
        }

        #endregion

        protected virtual void Attach()
        {
            MyCanvas.PointerPressed += HandlePointerPressed;
            MyCanvas.PointerMoved += HandlePointerMoved;
            MyCanvas.PointerReleased += HandlePointerReleased;
            MyCanvas.PointerExited += HandlePointerExited;
            MyCanvas.Tapped += HandleCanvasTapped;
            MyCanvas.DoubleTapped += HandleCanvasDoubleTapped;
        }

        protected virtual void Detach()
        {
            MyCanvas.PointerPressed -= HandlePointerPressed;
            MyCanvas.PointerMoved -= HandlePointerMoved;
            MyCanvas.PointerReleased -= HandlePointerReleased;
            MyCanvas.PointerExited -= HandlePointerExited;
            MyCanvas.Tapped -= HandleCanvasTapped;
            MyCanvas.DoubleTapped -= HandleCanvasDoubleTapped;
        }

        protected virtual void HandlePointerPressed(object sender, PointerRoutedEventArgs e)
        {
        }

        protected virtual void HandlePointerMoved(object sender, PointerRoutedEventArgs e)
        {
        }

        protected virtual void HandlePointerReleased(object sender, PointerRoutedEventArgs e)
        {
        }

        protected virtual void HandlePointerExited(object sender, PointerRoutedEventArgs e)
        {
        }

        protected virtual void HandleCanvasTapped(object sender, TappedRoutedEventArgs e)
        {
        }

        protected virtual void HandleCanvasDoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
        {
        }

        #endregion
    }
}
